// 生产token的jwt
const jwt = require('jsonwebtoken')

// 引入 goods 表
const setSlideImg = require('../../models/setSlideImg')

// 分页查询
listSlideImg = (req,res)=>{
    // console.log(req.query)
    const temp = req.headers.authorization
    const temp1 = temp.split(' ')
    const token = temp1[1]
    jwt.verify(token,'quan_and_huan',(err, authData) => {
        if(err) {
            res.json({code:598,msg:'token失效,请重新登录!'});
        }else{
            // token验证成功之后
            setSlideImg.find()
            .then(result=>{
                res.json({code:200,data:result})
            })
        }
    });
}
// 删除
deleteSlideImg = (req,res)=>{
    const temp = req.headers.authorization
    const temp1 = temp.split(' ')
    const token = temp1[1]
    jwt.verify(token,'quan_and_huan',(err, authData) => {
        if(err) {
            res.json({code:598,msg:'token失效,请重新登录!'});
        }else{
            setSlideImg.findByIdAndDelete({_id:req.params.id}).then(result=>{
                res.json({code:200,data:result})
            })
        }
    });
}
// 新增
addSlideImg = (req,res)=>{
    // console.log(req.body)
    const temp = req.headers.authorization
    const temp1 = temp.split(' ')
    const token = temp1[1]
    jwt.verify(token,'quan_and_huan',(err, authData) => {
        if(err) {
            res.json({code:598,msg:'token失效,请重新登录!'});
        }else{
            let _setSlideImg = {}
            _setSlideImg.slideImgUrl = req.body.slideImgUrl
            _setSlideImg.slideImg = req.body.slideImg
            new setSlideImg(_setSlideImg).save().then(result=>{
                // console.log(result)
                res.json({code:200,data:result})
            })
        }
    });
}

module.exports = {
    listSlideImg,deleteSlideImg,addSlideImg
}